package com.cn.bean;

import java.util.ArrayList;
import java.util.Date;

import net.sf.json.JSONObject;

import com.cn.xc.utils.ObjectUtil;

/**
 * @author sushy
 * @date 2016-3-23
 */
public class SimpleList extends ArrayList<Object> {

	private static final long serialVersionUID = 9015452425719238806L;

	public SimpleList() {
		super();
	}

	public SimpleList(int initialCapacity) {
		super(initialCapacity);
	}

	public int getInt(int index) {
		if (isOutOfBounds(index)) {
			return 0;
		}
		return getInt(index, 0);
	}

	public int getInt(int index, int defaultValue) {
		if (isOutOfBounds(index)) {
			return defaultValue;
		}
		return ObjectUtil.toInt(get(index), defaultValue);
	}

	public long getLong(int index) {
		if (isOutOfBounds(index)) {
			return 0;
		}
		return getLong(index, 0);
	}

	public long getLong(int index, long defaultValue) {
		if (isOutOfBounds(index)) {
			return defaultValue;
		}
		return ObjectUtil.toLong(get(index), defaultValue);
	}

	public double getDouble(int index) {
		if (isOutOfBounds(index)) {
			return 0;
		}
		return getDouble(index, 0);
	}

	public double getDouble(int index, double defaultValue) {
		if (isOutOfBounds(index)) {
			return defaultValue;
		}
		return ObjectUtil.toDouble(get(index), defaultValue);
	}

	public String getString(int index) {
		return getString(index, null);
	}

	public String getString(int index, String defaultValue) {
		if (isOutOfBounds(index)) {
			return defaultValue;
		}
		return ObjectUtil.toString(get(index), defaultValue);
	}

	public Date getDate(int index) {
		if (isOutOfBounds(index)) {
			return null;
		}
		return ObjectUtil.toDate(get(index));
	}

	public JSONObject toJson() {
		return JSONObject.fromObject(this);
	}

	public String toJsonString() {
		return toJson().toString();
	}

	private boolean isOutOfBounds(int index) {
		return (index < 0 || index >= size());
	}

}
